/* 
 * Copyright 2011 VelocitiSoftware
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package com.velociti.ikarus.widget.client.ui;

import java.util.ArrayList;
import java.util.List;

import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyPressEvent;
import com.google.gwt.event.dom.client.KeyPressHandler;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.user.client.ui.RootPanel;
import com.vaadin.terminal.gwt.client.ApplicationConnection;
import com.vaadin.terminal.gwt.client.UIDL;
import com.vaadin.terminal.gwt.client.VConsole;
import com.vaadin.terminal.gwt.client.ui.VTextField;

/* ######VIkarusTextField#####
 * @author Alper Turkyilmaz - VelocitiSoftware - 2011
 * @version 1.0.1
 */
public class VIkarusTextField extends VTextField implements IVClientSideAttachDetach {
	protected static final List<Integer> keyCodes = new ArrayList<Integer>();
	public static final String TEXTTYPE_PLAIN = "PLAIN";
	public static final String TEXTTYPE_ALPHANUMERIC = "ALPHANUMERIC";
	public static final String TEXTTYPE_DIGIT = "DIGIT";
	public static final String TEXTTYPE_LETTER = "LETTER";
	public static final char BLANK = ' ';

	private String textType = TEXTTYPE_PLAIN;
	private String exceptionalChars = "";
	protected HandlerRegistration handlerReg = null;

	static {
		keyCodes.add(KeyCodes.KEY_ALT);
		keyCodes.add(KeyCodes.KEY_BACKSPACE);
		keyCodes.add(KeyCodes.KEY_CTRL);
		keyCodes.add(KeyCodes.KEY_DELETE);
		keyCodes.add(KeyCodes.KEY_DOWN);
		keyCodes.add(KeyCodes.KEY_END);
		keyCodes.add(KeyCodes.KEY_ENTER);
		keyCodes.add(KeyCodes.KEY_ESCAPE);
		keyCodes.add(KeyCodes.KEY_HOME);
		keyCodes.add(KeyCodes.KEY_LEFT);
		keyCodes.add(KeyCodes.KEY_PAGEDOWN);
		keyCodes.add(KeyCodes.KEY_PAGEUP);
		keyCodes.add(KeyCodes.KEY_RIGHT);
		keyCodes.add(KeyCodes.KEY_SHIFT);
		keyCodes.add(KeyCodes.KEY_TAB);
		keyCodes.add(KeyCodes.KEY_UP);
	}

	private KeyPressHandler keyPressHandler;

	protected KeyPressHandler getKeyPressHandler() {
		return new KeyPressHandler() {
			public void onKeyPress(KeyPressEvent e) {
				if (!keyCodes.contains(e.getNativeEvent().getKeyCode())
						&& (exceptionalChars.indexOf(e.getCharCode()) == -1)
						&& ((textType.equals(TEXTTYPE_DIGIT) && !Character.isDigit(e.getCharCode()))
								|| (textType.equals(TEXTTYPE_ALPHANUMERIC) && e.getCharCode() != BLANK && !Character.isLetterOrDigit(e.getCharCode())) || (textType.equals(TEXTTYPE_LETTER)
								&& e.getCharCode() != BLANK && !Character.isLetter(e.getCharCode())))) {
					e.preventDefault();
					return;
				}
			}
		};
	}

	public VIkarusTextField() {
	}

	public void updateFromUIDL(UIDL uidl, ApplicationConnection client) {
		super.updateFromUIDL(uidl, client);
		if (client.updateComponent(this, uidl, false)) {
			return;
		}
		setTextType(uidl.getStringAttribute("textType"));
		String exceptionalChars = uidl.getStringAttribute("exceptionalChars");
		setExceptionalChars(exceptionalChars);
	}

	public String getTextType() {
		return textType;
	}

	public void setTextType(String textType) {
		this.textType = textType;
		if (!textType.equals(TEXTTYPE_PLAIN) && handlerReg == null) {
			handlerReg = addKeyPressHandler(getKeyPressHandler());
		} else if (textType.equals(TEXTTYPE_PLAIN) && handlerReg != null) {
			handlerReg.removeHandler();
		}

	}

	public String getExceptionalChars() {
		return exceptionalChars;
	}

	public void setExceptionalChars(String exceptionalChars) {
		this.exceptionalChars = exceptionalChars;

	}

	public void attach() {
		onAttach();
		RootPanel.detachOnWindowClose(this);
	}

	public void detach() {
		onDetach();
	}
}
